import asyncio

import aiohttp

urls = [
    'https://i1.huishahe.com/uploads/tu/201910/10219/3.jpg',
    'https://i1.huishahe.com/uploads/tu/201911/10098/2.jpg',
    'https://i1.huishahe.com/uploads/tu/201911/9999/rnd2c4032691.jpeg'
]
header = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36 Edg/122.0.0.0"
}


async def download(url):
    name = url.rsplit(r"/", 1)[1]
    # 1.发送请求 s = aiohttp.ClientSession() # 约等于requests()对象
    async with aiohttp.ClientSession() as session:
        # 2.处理请求
        async with session.get(url) as rsp:
            with open(name, mode='wb') as f:
                f.write(await rsp.content.read())  # 读取内容是异步的，需要await挂起

    print("OK")


async def main():
    tasks = []
    for imag in urls:
        tasks.append(download(imag))
    await asyncio.wait(tasks)


if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    loop.run_until_complete(main())